<?php    
/* 
* @package TDE Newsletter
* @version: 1.0 
* @last modified: 10:28 AM 06/15/2007
* http://www.topdreamweaverextensions.com
*/
  require_once "./../config.php";
  require_once "./language/".ADMIN_LANGUAGE.".inc.php";
  require_once("./editor/fckeditor.php");
  
  if ($_SESSION["session_username"] == '') 
      header("Location: index.php");

  if (isset($_POST["title"]))
  {
      
      $_POST["title"] = str_replace("\'","'", str_replace('\"','"', $_POST["title"]));
      $_POST["html"] = str_replace("\'","'", str_replace('\"','"', $_POST["html"]));
      
      @unlink(DATABASE_FOLDER."content_".$_GET["id"]);
          
      $id = base64_encode($_POST["cid"]);
          
      $fp = fopen(DATABASE_FOLDER."content_".$id, "w");
      $str = base64_encode($_POST["title"])."\n".
             base64_encode($_POST["html"])."\n";
      fwrite ($fp, $str);
      fclose ($fp);
	  header("Location: contents.php"); 
      
  }
  else 
  {
      if ($_GET["id"] != '')
      {
          $file = file(DATABASE_FOLDER."content_".$_GET["id"]);
          $item["title"] = base64_decode(trim($file[0]));
          $item["html"] = base64_decode(trim($file[1]));      
          $id = base64_decode($_GET["id"]);
      }
      else 
          $id = "";
  }    
       
?>
<?php require "./template/top.inc.php" ?>
<script language="JavaScript">
	
   function validate()
   {
       init_validate();
       isRequired ('cid', 'Content identifier');
       isRequired ('title', '<?php echo TXT_NEWSLETTERS_TITLE; ?>');
       if (allok())    
       {
           var st2 = document.FormEdit.cid.value;
           st2 = st2.toLowerCase();
<?php
           $path = DATABASE_FOLDER;
           $d = dir($path);
           $fnames = array();
           while (false !== ($entry = $d->read()))
               if ($entry != '.' && $entry != '..' && !is_dir($path.$entry) && substr($entry,0,8) == 'content_' )
                  $fnames[] =  $entry; 
           foreach ($fnames as $token)       
               if (substr($token,8) != $_GET["id"])
                   echo 'if ("'.strtolower(base64_decode(substr($token,8))).'" == st2) {alert("Content identifier already used. Please enter a new content identifier.");return;}';
?>
	         document.FormEdit.submit();
	     }
   }
   
   function testsubmit()
   {
	   return validate(); 
	   return false;
   }
   

   function cancelarFunction()
   {   
		window.location  = "contents.php";   
   }
   
</script>

<div id=content>
 <div id=breadCrumb><A><?php echo TXT_NEWSLETTER ?></A></div>
 <h2 id=pageName><?php if (isset($_GET["id"])) echo ALL_MSG_EDIT; else echo ALL_MSG_ADD; ?> <?php echo TXT_NEWSLETTER; ?></h2>

 <?php require "./template/middle.inc.php" ?>
 <div class=story>
      <table width="100%" border=0>
  <form name="FormEdit" method="post" action="edit_content.php?id=<?php echo $_GET["id"]; ?>" onsubmit="return testsubmit();" >      
  
        <tr> 
          <td colspan="2"><h6>Content Identifier:</h6></td>
        </tr>       
        <tr>
          <td class="tinytext" colspan="2">
            <div style="position:relative">
              <input size="20" maxlength="20" type="text" class="box" name="cid" value="<?php echo $id; ?>" />
            </div>  
            <em style="font-size:11px;">A short identifier that allows to add a reference to it from Dreamweaver, example: "homepage", "content1", "aboutus",...</em>
          </td>
        </tr>     
                

        <tr> 
          <td colspan="2"><h6><?php echo TXT_NEWSLETTERS_TITLE; ?>:</h6></td>
        </tr>       
        <tr>
          <td class="tinytext" colspan="2">
            <div style="position:relative">
              <input size="70" type="text" class="box" name="title" value="<?php echo @$item["title"]; ?>" />
            </div>  
            <em style="font-size:11px;">Your page's title</em>
          </td>
        </tr>
        
        <tr>
          <td colspan="2"><h6><?php echo TXT_NEWSLETTERS_HTML; ?>:</h6></td>
        </tr>       
        <tr>
          <td class="tinytext" colspan="2">
            <div style="position:relative">
<?php

$sBasePath = "./editor/" ;  // poner camino donde esta el fck editor aca, yo lo estaba poniendo en la raiz

$oFCKeditor = new FCKeditor('html');
$oFCKeditor->BasePath	= $sBasePath ;
$oFCKeditor->Height	= 300 ;
$oFCKeditor->Value		= @$item["html"];
$oFCKeditor->Create() ;
?>
            </div>  
          </td>
        </tr>
       <tr> 
          <td colspan="2"></td>
       </tr>       
        <tr> 
          <td><input type=button name=aceptar value="<?php echo ALL_MSG_OK; ?>" onclick="javascript:validate();" class=forminput /> 
            &nbsp; <input type=button name=cancelar value="<?php echo ALL_MSG_CANCEL; ?>" onclick="javascript:cancelarFunction();" class=forminput /> 
          </td>
        </tr>
  </form>        
      </table>
      
 </div>
</div>		      
<div id=navBar>
 <div class=relatedLinks>
 <h3><?php echo TXT_NEWSLETTERS; ?></h3>
 <ul>
   <LI><A href="contents.php"><?php echo TXT_NEWSLETTERS_LIST; ?></A></LI>
  </ul>
  <img src="/images//spacer.gif" height=60% width=1 />
 </div>
</div><br><br>
<?php require "./template/bottom.inc.php" ?>